//202102104001魏晓宇
package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.TeacherDao;
import cn.edu.sdjzu.xg.kcsj.domain.Teacher;
import cn.edu.sdjzu.xg.kcsj.domain.User;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class TeacherService {
    private static final TeacherDao teacherDao = TeacherDao.getInstance();
    private static final TeacherService teacherService = new TeacherService();

    private TeacherService() {
    }

    public static TeacherService getInstance() {
        return teacherService;
    }

    public Collection<Teacher> findAll() throws SQLException {
        return this.findAll(null, null);
    }

    public Collection<Teacher> findAll(Pagination pagination, List<Condition> conditionList) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = TeacherDao.getInstance().findAll(pagination, conditionList, connection);
        JdbcHelper.close(connection);
        return teachers;
    }


    public Teacher find(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Teacher teacher = teacherDao.find(id, conn);
        JdbcHelper.close(conn);
        return teacher;
    }

    public boolean update(Teacher teacher) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean updated = false;
        try {
            updated = teacherDao.update(teacher, conn);
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();

        } finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return updated;
    }

    public boolean deletes(List<Teacher> listToDelete) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            for (Teacher e : listToDelete) {
                int id = e.getId();
                deleted = teacherDao.delete(id, connection);
            }
            connection.commit();
        } catch (Exception e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean deleted = false;

        try {
            deleted = teacherDao.delete(id, conn);
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
        } finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return deleted;
    }

    /**
     * 添加Teacher对象
     *
     * @param teacher 要添加的Teacher对象
     * @return 添加成功返回true，否则false
     * @throws SQLException
     */
    public Boolean add(Teacher teacher) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean added = false;
        try {
            teacher.setId(ActorService.getInstance().add());
            User user = new User(teacher.getNo(), teacher.getNo(), 2, null, teacher);
            Integer id = UserService.getInstance().add(user);
            teacher.setId(id);

            added = teacherDao.add(teacher, connection);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return added;
    }

}
